<template>
  <div class="box">
    <div class="buttonClass">
      <el-button type="primary" round icon="el-icon-plus" size="small" style="margin: 10px 15px;"
                 @click="openDialogFrom()"
      >添加商品
      </el-button>
    </div>
    <div class="tableClass">
      <el-table
        :data="tradeList"
        border
        height="717"
      >
        <el-table-column
          type="index"
          label="序号"
          width="150"
          align="center"
        >
          <template slot-scope="row">
            {{ (pageNum - 1) * pageSize + row.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="tmName"
          label="商品名称"
          width="250"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="logoUrl"
          label="商品图片"
          min-width="200"
          align="center"
        >
          <template slot-scope="row">
            <img :src="row.row.logoUrl" height="40px">
          </template>
        </el-table-column>
        <el-table-column
          prop="address"
          label="操作"
          width="230"
          align="center"
        >
          <template slot-scope="{row,$index}">
            <el-button
              size="mini"
              @click="openDialogFrom(row)"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(row,$index)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :page-size.sync="pageSize"
        :page-sizes="[9, 18, 27]"
        :current-page.sync="pageNum"
        :pager-count="7"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      >
      </el-pagination>
    </div>
    <div class="dialogClass">
      <el-dialog :title="tradeForm.id?'修改商品':'添加商品'" :visible.sync="dialogFormVisible">
        <el-form :model="tradeForm" :rules="rules" ref="ruleForm">
          <el-form-item label="商品名称" prop="tmName">
            <el-input autocomplete="off" style="width: 20%" v-model="tradeForm.tmName"></el-input>
          </el-form-item>
          <el-form-item label="商品图片" prop="logoUrl">
            <el-upload
              class="avatar-uploader"
              action="/swagger-api/admin/product/fileUpload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <div class="el-upload__tip" slot="tip" style="margin-left: 73px">只能上传jpg/png文件，且不超过500kb</div>
              <img v-if="tradeForm.logoUrl" :src="tradeForm.logoUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>

            </el-upload>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveOrUpdateTradeMark">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getTradeMark, saveOrUpdateTrademark, deleteTrademark } from '@/api/swagger'

export default {
  name: 'TradeMark',
  data() {
    // 这里存放数据
    return {
      tradeList: [],
      pageSize: 9,
      pageNum: 1,
      total: 0,
      dialogFormVisible: false,
      tradeForm: {
        tmName: '',
        logoUrl: '',
        id: ''
      },
      rules: {
        tmName: [
          { required: true, message: '请输入商品名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'change' }
        ],
        logoUrl: [
          { required: true, message: '请选择logo图片', trigger: 'change' }
        ]
      }
    }

  },
  components: {},
  props: {},
  methods: {
    //分页查询list
    async getTradeList(pageSize, pageNum) {
      await getTradeMark(pageSize, pageNum).then(res => {
        this.tradeList = res.data.records
        this.total = res.data.total
        this.pageNum = pageNum
        this.pageSize = pageSize
      })
    },
    handleCurrentChange(pageNum) {
      this.getTradeList(this.pageSize, pageNum)
    },
    handleSizeChange(pageSize) {
      this.getTradeList(pageSize, 1)
    },
    handleAvatarSuccess(res) {
      this.tradeForm.logoUrl = res.data
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG/PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    openDialogFrom(tradeForm) {
      this.tradeForm.id = ''
      this.tradeForm.tmName = ''
      this.tradeForm.logoUrl = ''
      if (tradeForm) {
        this.tradeForm = { ...tradeForm }
      }
      this.dialogFormVisible = true
    },
    saveOrUpdateTradeMark() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          await saveOrUpdateTrademark(this.tradeForm).then(res => {
            if (res.code == 200) {
              this.dialogFormVisible = false
              this.getTradeList(this.pageSize, this.tradeForm.id ? this.pageNum : 1)
              this.$message({ message: this.tradeForm.id ? '修改商品成功' : '新增商品成功', type: 'success' })
            }
          })
        } else {
          return false
        }
      })

    },
    handleDelete(tradeForm, index) {
      this.$confirm(`您确定要刪除${tradeForm.tmName}吗？`, '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteTrademark(tradeForm.id).then(res => {
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getTradeList(this.pageSize, this.tradeList.length > 1 ? this.pageNum : (this.pageNum - 1) > 0 ? this.pageNum - 1 : 1)
          }
        })
      })
    }
  }
  ,
  mounted() {
    this.getTradeList(this.pageSize, this.pageNum)
  }

}
</script>

<style>
.buttonClass {
  border-bottom: 1px solid rgb(221, 221, 221);
  overflow: hidden;
  height: 52px;
  width: 100%;
  background: #fff;
  box-shadow: 0 1px 4px 0 #bac8dc;
  border-top: solid 1px #bac8dc;
}

.tableClass {
  width: 100%;
  text-align: center;
  padding: 15px;

}

.el-pagination {
  padding: 20px;
  background: #fff;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.dialogClass {
  width: 80%;
}

.el-form-item__error{
  left: 76px
}
</style>
